class_name EquippableComponent
extends Area2D

@export var weapon: WeaponBase

var canWeapon: bool = true


func _ready():
	# 确保连接了碰撞信号
	if not body_entered.is_connected(_on_body_entered):
		body_entered.connect(_on_body_entered)


func _on_body_entered(body: Node2D) -> void:
	# 检查进入的物体是否是玩家
	if body is Player:
		var player: Player = body as Player
		# 使用 call_deferred 来确保在帧处理结束后执行装备操作
		call_deferred("equip_weapon_to_player", player)


func equip_weapon_to_player(player_to_equip: Player) -> void:
	# 确保武器和武器持有者都有效
	if canWeapon:
		canWeapon = false
		player_to_equip.equip_weapon(weapon)
